﻿using Cms.Core.Application.IService.TokenInfo;
using Cms.Core.Data.Model;
using Cms.Core.Data.UnitOfWork;
using Cms.Core.Infrastructure.Common;
using Cms.Core.Infrastructure.Extension;
using Cms.Core.Infrastructure.Helps;
using Cms.Core.Infrastructure.Ui;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;

namespace Cms.Core.Application.Service.TokenInfo
{
    /// <summary>
    /// Token身份票
    /// </summary>
    public class TokenService : ITokenService
    {
        private readonly IRepository<SysToken> _sysTokenRepository = null;
        private readonly IUnitOfWork _unitOfWork=null;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="sysTokenRepository"></param>
        /// <param name="unitOfWork"></param>
        public TokenService(IRepository<SysToken> sysTokenRepository, IUnitOfWork unitOfWork)
        {
            _sysTokenRepository = sysTokenRepository;
            _unitOfWork = unitOfWork;
        }

        /// <summary>
        /// 得到Token
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        public async Task<TokenDto> GetToken(string token)
        {
            token = token?.Trim();
            return (await _sysTokenRepository.GetDtoAsync<TokenDto>(o => o.Token == token)) ?? new TokenDto();
        }


        /// <summary>
        /// 判断是否过期
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        public async Task<bool> IsTokenExpiredAsync(string token)
        {
            var queryDto = await this.GetToken(token);
            var nowtime = DateTime.Now;
            return !(queryDto.IsNotNull() && queryDto.ExpirationTime >= nowtime) ? true : false;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="input"></param>
        /// <param name="claims"></param>
        /// <returns></returns>
        public async Task<OperationResult> SaveTokenAsync(TokenDto input, IEnumerable<Claim> claims)
        {
            var clasimStrJson = claims.ToJson();

            return await _unitOfWork.Commit(async () =>
           {
               await _sysTokenRepository.SoftDeleteBatchAsync(o => o.CreateUserId == input.CreateUserId && o.IsDelete == false, m => new SysToken()
               {
                   IsDelete = true,
                   ModifiedTime = DateTime.Now,
               });
               return await _sysTokenRepository.InsertAsync(input);
           });

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        public async Task<OperationResult> SetTokenStateAsync(string token)
        {
            var id = (await _sysTokenRepository.QueryNotDelete.Where(o => o.Token == token).FirstOrDefaultAsync())?.Id;

            if (id.IsEmptyOrNullOrHasValue())
            {

                return new OperationResult("没有查询到对应的Token", OperationResultType.Error);
            }
            return await _sysTokenRepository.SoftDeleteAsync(id.Value);
        }
    }
}
